/**
 * 
 */
package com.jjt.kiki.dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.UUID;

import com.jjt.kiki.bean.No;
import com.jjt.kiki.bean.Res;

/**
 * @author jjtEatJava
 *
 */
public class ResDao extends BaseDao{
	public void save(List<Res> list) {
		Connection conn = null;
		PreparedStatement statement =null;
		try {
			conn = this.getConnection();
			statement = conn.prepareStatement("insert into tb_res (res_id,res_url,res_user_uk,res_title,res_cralwer_date,res_share_date,res_short_url,res_is_share) values(?,?,?,?,?,?,?,?)");
			for(Res res:list) {
				UUID uuid = UUID.randomUUID();
				statement.setString(1, uuid.toString());
				statement.setString(2, res.getResUrl());
				statement.setString(3,res.getResUserUk());
				statement.setString(4,res.getResTitle());
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				statement.setString(5, sdf.format(res.getResCralwerDate()));
				statement.setString(6, sdf.format(res.getResShareDate()));
				statement.setString(7,res.getResShortUrl());
				statement.setInt(8,res.getResIsShare());
				statement.addBatch();
			}
			statement.executeBatch();
			statement.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				statement.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	public int findByResShortUrl(Res res) {
		Connection conn = null;
		PreparedStatement statement =null;
		try {
			conn = this.getConnection();
			statement = conn.prepareStatement("select count(*) from tb_res where res_short_url = ?");
			statement.setString(1,res.getResShortUrl());
			ResultSet set = statement.executeQuery();
			set.next();
			int count = set.getInt(1);
			statement.close();
			conn.close();
			return count;
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			try {
				statement.close();
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return -1;
	}
}
